#include "QKoreaIdCreator.h"
#include <QDate>

QKoreaIdCreator::QKoreaIdCreator(QWidget *parent)
    : QMainWindow(parent)
{
    setFixedSize(250,320);
    mGenderGroup = new QGroupBox(this);
    mGenderGroup->setGeometry(10,10,150,50);
    mGenderGroup->setTitle("Gender");
    mManRadio = new QRadioButton(mGenderGroup);
    mManRadio->setText("Man");
    mManRadio->setGeometry(10,15,50,30);
    mManRadio->setChecked(true);
    mWomanRadio = new QRadioButton(mGenderGroup);
    mWomanRadio->setText("Woman");
    mWomanRadio->setGeometry(80,15,70,30);

    mCreatButton = new QPushButton(this);
    mCreatButton->setGeometry(170,25,70,30);
    mCreatButton->setText("Create");

    mCalendarWidget = new QCalendarWidget(this);
    mCalendarWidget->setGeometry(10,70,230,200);

    mResultLineEdit = new QLineEdit(this);
    mResultLineEdit->setGeometry(10,275,230,35);
    mResultLineEdit->setAlignment(Qt::AlignCenter);

    connect(mCreatButton,SIGNAL(clicked()),this,SLOT(createKoreadIdSlot()));

}

QKoreaIdCreator::~QKoreaIdCreator()
{
    
}

void QKoreaIdCreator::createKoreadIdSlot(){


    QDate date = mCalendarWidget->selectedDate();
    QString dateString = date.toString("yyMMdd");
    int year1 = (int)dateString.at(0).toAscii()-0x30;
    int year2 = (int)dateString.at(1).toAscii()-0x30;
    int month1 = (int)dateString.at(2).toAscii()-0x30;
    int month2 = (int)dateString.at(3).toAscii()-0x30;
    int day1 = (int)dateString.at(4).toAscii()-0x30;
    int day2 = (int)dateString.at(5).toAscii()-0x30;

    int gender;
    if(mManRadio->isChecked()){
        gender = 1;
    }else{
        gender = 2;
    }

    int local1 = qrand()%10;
    int local2 = qrand()%10;
    int local3 = qrand()%10;
    int local4 = qrand()%10;
    int order = (qrand()%3)+1;
    int last = 11-(((year1*2)+(year2*3)+(month1*4)+(month2*5)+(day1*6)+(day2*7)+(gender*8)+(local1*9)+(local2*2)+(local3*3)+(local4*4)+(order*5))%11);

    int code = gender*1000000+local1*100000+local2*10000+local3*1000+local4*100+order*10+last;

    mResultLineEdit->setText(QString(dateString+" - "+"%1").arg(code,0,10));


}
